<template>
  <div class="detailInfo" ref="containerRef">
    <!-- 标题 -->
    <div class="title">{{ title }}</div>
    <!-- 标签 -->
    <div class="tagList">
      <div
        class="tag"
        v-for="(data, index) in tagList"
        :key="index"
        :style="{ backgroundColor: data.color }"
      >
        <!--boxShadow: ` 0px 0px 5px 1px ${data.color}`, -->
        {{ data.tag }}
      </div>
    </div>
    <!-- 图表 -->
    <div>
      <el-table :data="detailInfoDataProp" stripe>
        <el-table-column prop="key" label="项目" :width="tableWidth" />
        <el-table-column prop="value" label="属性值" :width="tableWidth" />
      </el-table>
    </div>
    <!-- 按钮 -->
    <div>
      <div class="button" @click="returnBack">
        <img src="@/assets/images/icon/infoList/返回.png" /> 返回
      </div>
    </div>
  </div>
</template>

<script>
/*eslint-disable*/
import { getCurrentInstance, onMounted, ref, watch } from "vue";
import pubsub from "pubsub-js";
import { useRoute, useRouter, historyStack } from "vue-router";
import Message from "@/components/Message/showMessage";

export default {
  name: "详细信息",
  data() {
    return {
      tableWidth: 0,
      detailInfoDataProp: [],
      title: "",
      tagList: [],
    };
  },
  setup() {
    const { proxy } = getCurrentInstance();
    const containerRef = ref(null);
    const route = useRoute();
    const router = useRouter();

    watch(
      // 路由更新监听
      () => route.params,
      (newParams) => {
        // console.log(newParams);
        if (isEmptyObject(newParams)) {
          return;
        } else {
          // 信息填入
          // proxy.detailInfoDataProp = JSON.parse(newParams.detailInfoDataProp);
          dataHandle();
        }
      }
    );

    onMounted(() => {
      // 错误路由参数
      if (isEmptyObject(route.params)) {
        Message("路由参数错误，即将返回首页");
        returnBack();
      } else {
        // 信息填入
        dataHandle();
        // proxy.detailInfoDataProp = JSON.parse(route.params.detailInfoDataProp);
        // 地图样式更改
        pubsub.publish("mapExpand", "dataStyle");
        // 元素尺寸
        setTimeout(() => {
          proxy.tableWidth = containerRef.value.offsetWidth * 0.5 - 10;
        }, 300);
      }
    });

    // 信息处理及填入
    function dataHandle() {
      // 总体数据
      let data = JSON.parse(route.params.detailInfoDataProp);
      // 图表数据
      proxy.detailInfoDataProp = data.result;
      // 标题数据
      proxy.title = data.title;
      // 标签数据
      proxy.tagList = data.tagList;
      // console.log(data);
    }

    // 验证空对象
    function isEmptyObject(obj) {
      return Object.keys(obj).length === 0 && obj.constructor === Object;
    }

    // 路由返回
    function returnBack() {
      // popup删除
      pubsub.publish("deleteMarker", ["namePopup"]);
      // 地图样式更改
      pubsub.publish("mapExpand", "mapStyle");
      // 读取上一个路由
      let lastRoute = window.historyStack[window.historyStack.length - 1];
      if (lastRoute.name === "detailInfo") {
        router.push({ name: "map" });
      } else {
        // 路由回退
        router.back();
      }
    }

    return {
      containerRef,
      returnBack,
    };
  },
};
</script>

<style lang="less" scoped>
.detailInfo {
  border: #3f69e4 1px solid;
  border-radius: 5px;
  width: 49.75%;
  height: 100%;
  position: relative;
  right: -0.25%;
  padding: 5px;
  > .title {
    color: #373737;
    font-weight: bold;
    font-size: 200%;
    font-family: "方正小标宋简体";
    padding: 0.5%;
  }
  > .tagList {
    display: flex;
    align-items: center;
    flex-direction: row;
    margin-bottom: 1%;
    > .tag {
      border-radius: 5px;
      padding: 3px 10px;
      color: white;
      margin: 0 0.5%;
    }
  }
}

.button {
  width: 100px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  background-color: #3f69e4;
  border-radius: 5px;
  margin-top: 1%;
  > img {
    width: 20px;
    transition: width 0.3s ease;
    margin-right: 5px;
  }
}

.button:hover {
  cursor: pointer;
}
.button:hover > img {
  width: 25px;
}
</style>